package cn.chengpengper.unit19.client.handler;

import cn.chengpengper.unit19.protocol.response.LoginResponsePacket;
import cn.chengpengper.unit19.protocol.session.Session;
import cn.chengpengper.unit19.util.SessionUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;


/**
 * @author huchengpeng
 */
@ChannelHandler.Sharable
public class LoginResponseHandler extends SimpleChannelInboundHandler<LoginResponsePacket> {

    public static final LoginResponseHandler INSTANCE = new LoginResponseHandler();

    protected LoginResponseHandler(){}

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginResponsePacket loginResponsePacket) throws Exception {
        if (loginResponsePacket.isSuccess()) {
            System.out.println("客户端登录成功,userId:" + loginResponsePacket.getUserId());
            SessionUtil.bindSession(new Session(loginResponsePacket.getUserId(), loginResponsePacket.getUserName()), ctx.channel());
        } else {
            System.out.println("客户端登录失败，" + loginResponsePacket.getReason());
        }
    }
}
